﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Collections.Concurrent;


namespace Moop.Common
{
    public static class Utils
    {

        public static IEnumerable<TSource> DistinctBy<TSource, TKey> (this IEnumerable<TSource> source, Func<TSource, TKey> keySelector)
        {
            HashSet<TKey> seenKeys = new HashSet<TKey>();
            foreach (TSource element in source)
            {
                if (seenKeys.Add(keySelector(element)))
                {
                    yield return element;
                }
            }
        }

        public static string EncloseInSquareParenthesis(this string inputString)
        {
            return "[" + inputString + "]";
        }


        public static string LastOlapMemberInPath(this string inputString)
        {
            if (inputString == "(All)")
            {
                return inputString;
            }
            else
            {
                int lastIndexOfSqBkts = inputString.LastIndexOf("[") + 1;
                int len = inputString.Length - lastIndexOfSqBkts - 1;

                return inputString.Substring(lastIndexOfSqBkts, len);
            }
        }

    }
}
